<?php

namespace App\Http\Controllers;


use App\Client;
use App\Good;
use App\Goods;
use App\Goodsfirst;
use App\Order;
use Carbon\Carbon;
use App\OrderGoods;
use Illuminate\Http\Request;


class OrderController extends Controller
{

    public function getAll($company) {
        $goods=Order::where('company',$company)->get();
        return response()->json($goods);
    }
    public function getCurrent($page,$company) {
        $pages=$page*10-10;
        $goods=Order::where('company',$company)->orderBy('id','desc')->skip($pages)->take(10)->get();
        return response()->json($goods);
    }
    public function getCount($company) {
        $goods=Order::where('company',$company)->count();
        return response()->json($goods);
    }
    public function getCurrentState($page,$company,$state) {
        $pages=$page*10-10;
        $goods=Order::where('company',$company)->where('state',$state)->skip($pages)->take(10)->get();
        return response()->json($goods);
    }
    public function getCountState($company,$state) {
        $goods=Order::where('company',$company)->where('state',$state)->count();
        return response()->json($goods);
    }
    public function one($id) {
        $order=Order::find($id);
        return response()->json($order);
    }
    public function search($word,$type,$company) {

        $order=Order::where('company',$company)->where($type,'like','%'.$word.'%')->orderBy('id','desc')->get();
        return response()->json($order);

    }


    public function del ( Request $request)
    {
        $date = $request->input('date');

        foreach ($date as $d) {
            Order::destroy($d['id']);
        }
        return response()->json('删除成功');

    }
    public function getNew()
    {
        $orders=Order::where('alloted',0)->where('type','>','0')->where('log','!=','8')->get();
        $new=array();
        $i=0;
        foreach ($orders as $item) {
            $new[]=$item;
            $new[$i]['check']=false;
            $i=$i++;
        }
        return response()->json($new);
    }

   
    public function add(Request $request)
    {

        $order=$request->input('order');
        $goods=$request->input('goods');
        $clientId=$order['clientId'];
        $client=Client::find($clientId);
        $order['clientName']=$client['name'];
        $order['orderNo']='DG'.$order['type'].time().rand(100,999);
        $order['date']=Carbon::now();
        $order['state']=2;
        $order['other1']="后台现货";
        if($request->input('orderFlow') == '订货'){
            $order['state']=1;
            $order['other1']="等待买手确认";
        }

        $order['cost']=$goods['salePrice']*$order['count'];
        $newOrder=Order::create($order);
        $orderGoods=$goods;
        $orderGoods['orderId']=$newOrder->id;
        $orderGoods['company']=$newOrder->company;
        $orderGoods['goodsId']=$goods['id'];
        $orderGoods['count']=$order['count'];
        $orderGoods['cost']=$order['cost'];
        OrderGoods::create($orderGoods);
    }
 public function addNewGoods(Request $request)
    {

        $order=$request->input('order');
        $goods=$request->input('goods');
        $imgs=$request->input('imgs');
        $clientId=$order['clientId'];
        $client=Client::find($clientId);
        $order['clientName']=$client['name'];
        $order['orderNo']='DG'.$order['type'].time().rand(100,999);
        $order['date']=Carbon::now();
        $order['state']=1;
        $order['other1']="等待买手确认";

        $newOrder=Order::create($order);
        $i=1;
        foreach ($imgs as $img){
            if($i== 1){
                $goods['img']=$img;
            }elseif($i == 2){
                $goods['img1']=$img;
            }elseif(($i == 3)){
                $goods['img2']=$img;
            }else{
                break;
            }
            $i=$i+1;
        }
        $goods['code']='code'.time().rand(100,999);
        $orderGoods=$goods;
        $newGoods=Goods::create($goods);
        $orderGoods['orderId']=$newOrder->id;
        $orderGoods['company']=$newOrder->company;
        $orderGoods['goodsId']=$newGoods->id;

        OrderGoods::create($orderGoods);
    }

    public function destroy(Request $request)
    {

        $orders=$request->input('date');
        foreach ($orders as $d){
            Order::destroy($d['id']);
        }
        return response()->json('删除成功');
    }

    public function newConfirm(Request $request, $id)
    {

        $date=$request->input('date');
        Order::where('id',$id)->update(['other1'=>"订单确认",'state'=>1,'costType'=>$date['costType'],'imprest'=>$date['imprest']]);
        return response()->json('商品确认成功');

    }

    public function haveGoods(Request $request, $id)
    {
        $date=$request->input('date');
        $inPrice=$request->input('inPrice');
        $buyerId=$request->input('buyerId');
        $buyerName=$request->input('buyerName');

            $order=Order::find($id);
            if($order){
                if ($order->state != 1 ){
                    return response()->json('订单状态不正确');

                }else{
                    $orderGoods=OrderGoods::where('orderId',$id)->first();

                    if ($orderGoods){
                        $orderGoods->inPrice=$inPrice;
                        $orderGoods->save();
                        Goods::where('id',$orderGoods->goodsId)->update(['inPrice'=>$inPrice]);

                        Order::where('id',$id)->update(['pay'=>$inPrice,'other1'=>"有货确认，等待发货",'state'=>2,'buyerId'=>$buyerId,'buyerName'=>$buyerName]);
                        return response()->json('订单确认成功');

                    }else{
                        return response()->json('订单商品不存在');
                    }

                }

            }else{
                return response()->json('订单不存在');
            }


    }
    public function noGoods(Request $request, $id)
    {

        $buyerId=$request->input('buyerId');
        $buyerName=$request->input('buyerName');

        $order=Order::find($id);
        if($order){
            if ($order->state != 1 ){
                return response()->json('订单状态不正确');

            }else{
                Order::where('id',$id)->update(['other1'=>'无货确认，订单结束','state'=>8,'buyerId'=>$buyerId,'buyerName'=>$buyerName]);
                return response()->json('无货确认成功');
            }

        }else{
            return response()->json('订单不存在');
        }

    }

    public function outConfirm(Request $request, $id)
    {
        $date=$request->input('date');
        $order=Order::find($id);
        if($order){
            if ($order->state != 2 ){
                return response()->json('订单状态不正确');
            }else{
                if ($date['expressType'] == '直邮'){
                    Order::where('id',$id)->update($date);
                    Order::where('id',$id)->update(['other1'=>"意大利发客户，等待客户收货",'state'=>5]);
                    return response()->json('发货信息录入成功');
                }else{
                    Order::where('id',$id)->update($date);
                    Order::where('id',$id)->update(['other1'=>"意大利发国内，等待国内收货",'state'=>3]);
                    return response()->json('发货信息录入成功');
                }
            }

        }else{
            return response()->json('订单不存在');
        }

    }
    public function chinaGetConfirm(Request $request, $id)
    {
        $date=$request->input('date');
        $order=Order::find($id);
        if($order){
            if ($order->state != 3 ){
                return response()->json('订单状态不正确');
            }else{
                    Order::where('id',$id)->update(['other1'=>"国内收货确认，等待国内发货",'state'=>4]);
                    return response()->json('国内收货确认成功');
            }
        }else{
            return response()->json('订单不存在');
        }

    }
    public function chinaOutConfirm(Request $request, $id)
    {
        $date=$request->input('date');
        $order=Order::find($id);
        if($order){
            if ($order->state != 4 ){
                return response()->json('订单状态不正确');
            }else{
                    Order::where('id',$id)->update($date);
                    Order::where('id',$id)->update(['other1'=>"国内发客户，等待客户收货",'state'=>5]);
                    return response()->json('国内发客户成功');

            }

        }else{
            return response()->json('订单不存在');
        }

    }
    public function costConfirm(Request $request, $id)
    {
        $receipt=$request->input('receipt');
        $receipt['receiptDate']=date('Y-m-d',strtotime($receipt['receiptDate']));
        $order=Order::find($id);
        if($order){
            Order::where('id',$id)->update($receipt);
            Order::where('id',$id)->update(['other1'=>"订单完成",'state'=>9,'costState'=>1,'receiptState'=>1]);
            return response()->json('收款成功');
        }else{
            return response()->json('订单不存在');
        }

    }
    public function payConfirm(Request $request, $id)
    {
        $order=Order::find($id);
        if($order){
            if ($order->state < 3 ){
                return response()->json('订单状态不正确');
            }else{
                Order::where('id',$id)->update(['payState'=>1]);
                return response()->json('付款成功');

            }

        }else{
            return response()->json('订单不存在');
        }

    }


}
